
package com.kanjian.music.util;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.support.v4.app.NotificationCompat;
import android.widget.RemoteViews;

import com.android.volley.Response;
import com.android.volley.toolbox.ImageRequest;
import com.kanjian.music.KanjianApplication;
import com.kanjian.music.R;
import com.kanjian.music.activity.MainActivity;
import com.kanjian.music.constants.IntentConstants;
import com.kanjian.music.entity.Live;
import com.kanjian.music.entity.Music;
import com.kanjian.music.volley.VolleyQueue;

/**
 * 通知栏工具类
 * 
 * @author xuyanjun@kanjian.com
 */
@SuppressLint("NewApi")
public class NotificationUtil {

    private static final int PLAYER_CONTROLLER_NOTIFICATION_ID = 20148508;

    private static final int LIVE_ALARM_NOTIFICATION_ID = PLAYER_CONTROLLER_NOTIFICATION_ID + 1;

    private static Bitmap mBitmap;

    private static NotificationManager mNotificationMgr = (NotificationManager) KanjianApplication
            .getContext().getSystemService(Context.NOTIFICATION_SERVICE);

    private static NotificationCompat.Builder mNotificationBuilder;

    private static Notification mNotification;

    static RemoteViews rootView;

    static RemoteViews bigView;

    static {
        rootView = new RemoteViews(KanjianApplication.getContext().getPackageName(),
                R.layout.notification_player_controller);
        bigView = new RemoteViews(KanjianApplication.getContext().getPackageName(),
                R.layout.notification_player_controller_bigview);

        Intent intent = new Intent(KanjianApplication.getContext(), MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_TASK_ON_HOME);
        PendingIntent pendingIntent = PendingIntent.getActivity(KanjianApplication.getContext(), 0,
                intent, 0);
        mNotificationBuilder = new NotificationCompat.Builder(KanjianApplication.getContext())
                .setContent(rootView).setSmallIcon(R.drawable.ic_launcher).setOngoing(true)
                .setContentIntent(pendingIntent);
        mNotification = mNotificationBuilder.build();
        mNotification.contentView = rootView;
        if (android.os.Build.VERSION.SDK_INT >= 16) {
            mNotification.bigContentView = bigView;
        }
    }

    public static void showCurrentPlayingMusic() {

        // if (mBitmap != null && !mBitmap.isRecycled()) {
        // mBitmap.recycle();
        // mBitmap = null;
        // }

        Music music = PlayerUtil.getCurrentPlayingMusic();

        // 设置文字
        rootView.setTextViewText(R.id.notify_musician_name, music.getUserName());
        rootView.setTextViewText(R.id.notify_music_name, music.getMusicName());
        bigView.setTextViewText(R.id.notify_musician_name, music.getUserName());
        bigView.setTextViewText(R.id.notify_music_name, music.getMusicName());

        // 设置图片
        if (mBitmap != null && !mBitmap.isRecycled()) {
            rootView.setImageViewBitmap(R.id.notify_musician_pic, mBitmap);
            bigView.setImageViewBitmap(R.id.notify_musician_pic, mBitmap);
        } else {
            rootView.setImageViewResource(R.id.notify_musician_pic, R.drawable.default_image_small);
            bigView.setImageViewResource(R.id.notify_musician_pic, R.drawable.default_image_small);
        }

        // 设置点击事件
        // 上一首
        Intent preIntent = new Intent(IntentConstants.ACTION_NOTIFICATION_ACTION_PRE);
        rootView.setOnClickPendingIntent(R.id.notify_pre_music, PendingIntent.getBroadcast(
                KanjianApplication.getContext(), 0, preIntent, PendingIntent.FLAG_UPDATE_CURRENT));
        bigView.setOnClickPendingIntent(R.id.notify_pre_music, PendingIntent.getBroadcast(
                KanjianApplication.getContext(), 0, preIntent, PendingIntent.FLAG_UPDATE_CURRENT));
        // 暂停播放
        Intent playPauseIntent = new Intent(IntentConstants.ACTION_NOTIFICATION_ACTION_PLAY);
        rootView.setOnClickPendingIntent(R.id.notify_play_pause_music, PendingIntent.getBroadcast(
                KanjianApplication.getContext(), 0, playPauseIntent,
                PendingIntent.FLAG_UPDATE_CURRENT));
        bigView.setOnClickPendingIntent(R.id.notify_play_pause_music, PendingIntent.getBroadcast(
                KanjianApplication.getContext(), 0, playPauseIntent,
                PendingIntent.FLAG_UPDATE_CURRENT));
        // 下一首
        Intent nextIntent = new Intent(IntentConstants.ACTION_NOTIFICATION_ACTION_NEXT);
        rootView.setOnClickPendingIntent(R.id.notify_next_music, PendingIntent.getBroadcast(
                KanjianApplication.getContext(), 0, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT));
        bigView.setOnClickPendingIntent(R.id.notify_next_music, PendingIntent.getBroadcast(
                KanjianApplication.getContext(), 0, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT));

        VolleyQueue.getImageRequestQueue().add(
                new ImageRequest(music.getUserPic(), new Response.Listener<Bitmap>() {
                    @Override
                    public void onResponse(Bitmap bmp) {
                        if (bmp != null) {
                            mBitmap = bmp;
                            rootView.setImageViewBitmap(R.id.notify_musician_pic, mBitmap);
                            bigView.setImageViewBitmap(R.id.notify_musician_pic, mBitmap);
                            mNotificationMgr.notify(PLAYER_CONTROLLER_NOTIFICATION_ID,
                                    mNotification);
                        }
                    }
                }, 0, 0, Config.ARGB_8888, null));
        mNotificationMgr.notify(PLAYER_CONTROLLER_NOTIFICATION_ID, mNotification);
    }

    public static void clearPlayingMusic() {
        mNotificationMgr.cancel(PLAYER_CONTROLLER_NOTIFICATION_ID);
    }

    public static void showLiveAlarmNotification(Live live) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(
                KanjianApplication.getContext());
        RemoteViews view = new RemoteViews(KanjianApplication.getContext().getPackageName(),
                R.layout.notification_live_alarm);
        Intent intent = new Intent();
        PendingIntent pendingIntent = PendingIntent.getActivity(KanjianApplication.getContext(), 0,
                intent, 0);
        builder.setSmallIcon(R.drawable.ic_launcher).setContent(view).setOngoing(false)
                .setContentIntent(pendingIntent);
        Notification n = builder.build();
        n.contentView = view;
        mNotificationMgr.notify(LIVE_ALARM_NOTIFICATION_ID, n);
    }

    public static void clearLiveAlarmNotification() {
        mNotificationMgr.cancel(LIVE_ALARM_NOTIFICATION_ID);
    }

}
